<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/3
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;

use think\Db;
use think\Config;
/**
 * 订单
 */
class Order extends Tpmecms
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

  
    public function _initialize()
    {
        parent::_initialize();

        
    }

    /**
     * 添加订单
     *
     * @param string $json json格式
     * @return void
     */
    public function add_order($json=null)
    {
        $json = html_entity_decode($json);
        // halt($json);
        $res = json_decode($json,true);



        $order_data['order'] = date('Y_m_d_H_i_s_')."U_".$this->auth->id.'_'.rand(0000,9999);//$this->rand_order('Order',$this->auth->id);//
        $order_data['uid'] =  $this->auth->id;//


        // $order_data['json'] =  '';//
        // $order_data['text'] =  '';//

        $order_data['addr_id'] =  $this->GetUserAddr($this->auth->id);//
        $order_data['status'] =  '1';//支付状态:1=已提交,2=已完成,3=取消订单,4=异常订单


        $order_data['time'] =  time();//
        $id = $order_data['id'] = Db::name('order')->insertGetId($order_data);


        foreach($res as $k=>$v){

            foreach($v['goods'] as $k2=>$font){
                unset($sub);
                $sub['x'] = $font['x'];
                $sub['y'] = $font['y'];
                $sub['font'] = $font['font'];
                $sub['initial'] = $font['initial'];
                $sub['num'] = $font['num'];
                $sub['font_id'] = $font['id'];
    
                $sub['p_id'] = $v['p_id'];
                $sub['b_id'] = $font['b_id'];
    
                $sub['order_id'] = $id;
                $res = Db::name('suborder')->where($sub)->find();
                if(!$res){
                    Db::name('suborder')->insert($sub);
                }
            }
        }
        $this->success('ok',$order_data);
       


    }


   
    /**
     * 获取订单列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $status 支付状态:1=已提交,2=已完成,3=取消订单,4=异常订单
     * @return void
     */
    public function get_order_list($page=1,$limit=10,$status=null)
    {
        $where['uid'] = $this->auth->id;
        if($status)
        $where['status'] = $status;
        $list = Db::name('order')->where($where)->page($page,$limit)->select();
        foreach ($list as $key => &$val) {
            $val['count'] = Db::name('suborder')->where('order_id',$val['id'])->count();
        }
        $this->success('ok',$list);
    }

    /**
     * 获取订单信息
     *
     * @param int $id 订单id
     * @return void
     */
    public function get_order_data($id)
    {
        $where['uid'] = $this->auth->id;
        $where['id'] = $id;
        $res = Db::name('order')->where($where)->find();
        if(!$res){
            $this->error('没有找到');
        }
        $res['sub'] = Db::name('suborder')->where('order_id',$id)->select();
        $res['b'] =Db::name('b')->find($res['sub'][0]['b_id']);
        $this->success('ok',$res);
    }

    /**
     * 取消订单
     *
     * @param int $id 订单id
     * @return void
     */
    public function cancel_order($id)
    {
        // @param string $status 支付状态:1=已提交,2=已完成,3=取消订单,4=异常订单
        $where['uid'] = $this->auth->id;
        $where['id'] = $id;
        $res = Db::name('order')->where($where)->find();
        if(!$res){
            $this->error('没有找到');
        }

        if($res['status']=='2'){
            $this->error('已发货不能取消订单');
        }

        if(($res['time']+24*3600)>time()){
            $res = Db::name('order')->where('id',$id)->update(['status'=>'3']);
            if($res){
                $this->success('取消成功');
            }
        }else{
            $this->error('时间已过24小时不能取消');
        }



        $this->error('请刷新订单数据');

    }

}